<?php

// 平台
class JdRankAction extends Action {


    private $cJdRank    = null;
    private $cLogs      = null;
    private $cJdRankLog = null;
    private $cAdmin		 = null;
    private $cJdSearch  = null;

	function __construct(){

		//判断管理员登录状态
		admin_login_check();
		
		import('@.ORG.Get');
		import('@.ORG.Post');
        import('@.ORG.JdRank');
        import('@.ORG.JdRankLog');
        import('@.ORG.Logs');
        import('@.ORG.Admin');
        import('@.ORG.Curl');
        import('@.ORG.JdSearch');

        $this->cLogs   = new Logs();
        $this->cJdRank = new JdRank();
        $this->cJdRankLog = new JdRankLog();
        $this->cAdmin 		= new Admin();

	}
	
	//列表
    public function index(){

        admin_role('JdRank:index');

        $get = get(array(
            array('ids','','')
        ));

        $ids = $get['ids'];

        $ids_array = array();
        if($ids != ''){
            //数组
            $ids_array = explode(',',$ids);
        }

        $admin_id = admin_id_get();

        //超管,可以看全部的，其它人只能看自己的
        $is_super_admin = $this->cAdmin->is_group_code('super_admin');
        if($is_super_admin){
            $admin_id = null;
        }

    	$ranks = $this->cJdRank->get_by_admin_id($admin_id);

        foreach($ranks as $i => $r){
            $r['checked'] = in_array($r['id'],$ids_array);
            $ranks[$i] = $r;
        }

    	$this->assign(
    		array(
    			'APP_NAME'      => APP_NAME,
    			'ranks'	        => $ranks,
                'ids'           => $ids,
                'ids_array'     => $ids_array,
    			'MainPage'		=> 'JdRank:index'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add(){

        admin_role('JdRank:index');

        $this->assign(
    		array(
    			'APP_NAME'	    => APP_NAME,
                'MainPage'		=> 'JdRank:add'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }


    public function add_do(){

        admin_role('JdRank:index');

        $admin_id = admin_id_get();
    	
   		$post = post(array(
    		array('item_id'		        ,'intval'   ,'0'),
            array('url'		            ,'trim',    ''),

            array('goods_name'		    ,'trim',    ''),
            array('shop_name'		    ,'trim',    ''),
            array('rank'		        ,'intval',  0),
            array('page'	            ,'',        5),
            array('search_rank'	        ,'intval',  0)
    	));

        $item_id    = $post['item_id'];
        $url        = $post['url'];

        $goods_name = $post['goods_name'];
        $shop_name  = $post['shop_name'];
        $rank       = $post['rank'];
        $search_rank= $post['search_rank'];

        $page       = $post['page'];

        //商品，店铺数据
        //$goods_info = $this->cJdRank->get_item_info_from_web($item_id);

        //排名
        //$rank = $this->cJdRank->get_item_rank($item_id,$url,$page);


        $data = array(
            'item_id'    => $item_id,
            'url'        => $url,
            'rank'       => $rank,
            'search_rank'=> $search_rank,
            'search_date'=> full_date(),
            'admin_id'   => $admin_id,
            'created'    => full_date(),
            'shop_name'  => $shop_name,
            'page'       => $page,
            'goods_name' => $goods_name
        );

    	$add_result = $this->cJdRank->add($data);
    
    	if($add_result > 0){

            //添加当天的日志
            $id = $add_result;
            $this->cJdRankLog->add_by_created($id,$rank);

    		$this->cLogs->add('JdRank:add','添加排名分析成功'.$item_id);
    		
    		json_success('添加排名分析成功');
    	}else{
    		json_error('添加排名分析成功失败');
    	}
    }


    /**
     * 查询排名
     */
    public function search_result(){

        admin_role('JdRank:index');

        $get = get(array(
            array('item_id'		        ,'intval'   ,'0'),
            array('url'		            ,'trim',    ''),
            //array('keywords'            ,'trim',    ''),
            array('page'	            ,'',        3)
        ));

        $item_id    = $get['item_id'];
        $url        = $get['url'];
        $page       = $get['page'];
        //$keywords   = $get['keywords'];

        //商品，店铺数据
        $goods_info = $this->cJdRank->get_item_info_from_web($item_id);


        //如果不填，则使用获取的
        if($url == ''){
            $url = $goods_info['url'];
        }

        //排名
        $rank = $this->cJdRank->get_item_rank($item_id,$url,$page);

        //搜索页排名
        /*
        $search_rank = 0;
        $search_date = '';
        if(!empty($keywords)){
            $this->cJdSearch    = new JdSearch($item_id,$keywords);
            $search_rank = $this->cJdSearch->get_rank();
            $search_date = full_date();
        }
        */

        $data = array(
            'item_id'    => $item_id,
            'url'        => $url,
            'rank'       => $rank,
            'created'     => full_date(),
            //'search_rank' =>$search_rank,
            //'search_date'=> $search_date,
            'shop_name'  => $goods_info['shop_name'],
            'goods_name' => $goods_info['goods_name']
        );

        $ranks[] = $data;

        $this->assign(
            array(
                'ranks'		=> $ranks
            )
        );

        $this->display('JdRank:search_result');
        exit;

    }

    
    //详情或修改
    public  function detail(){

        admin_role('JdRank:index');

    	$get = get(array(
    		array('id','intval',0)
    	));

    	$id = $get['id'];
    	
    	$jdRank = $this->cJdRank->get_by_id($id);
    	
    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,
    			'jdRank'		=> $jdRank,
    			'MainPage'		=> 'JdRank:detail'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }

    /**
     * 更新
     */
    public function update(){

        admin_role('JdRank:index');

    	$post = post(array(
    		array('id'		,'intval',0)
    	));

    	$id = $post['id'];

    	$update_result = $this->cJdRank->update($id,$post);

    	if($update_result){

    		$this->cLogs->add('JdRank:edit','排名分析更新成功:'.$id);

    		json_success('排名分析更新成功');
    	}else{
    		json_error('排名分析更新失败');
    	}
    }

    /**
     * 排名分析删除
     */
    function delete(){

        admin_role('JdRank:index');

        $post = post(array(
            array('id','intval',0)
        ));

        $id = $post['id'];

        //删除排名
        $del_rank = $this->cJdRank->delete_by_id($id);

        //删除记录
        $del_rank_log = $this->cJdRankLog->delete_by_rank_id($id);

        if($del_rank > 0){
            json_success('排名分析删除成功');
        }else{
            json_error('排名分析删除失败');
        }
    }

    /**
     * 排名js数据
     */
    public function rank_xml_data(){

        admin_role('JdRank:index');

        $get = get(array(
            array('ids','','')
        ));

        $ids = $get['ids'];

        $js = $this->cJdRank->items_rank_js($ids);

        echo $js;
    }
}